package com.wbao.dianniu.ui;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.lhh.apst.library.CustomPagerSlidingTabStrip2;
import com.lhh.apst.library.ViewHolder;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.tabstrip.fragments.APSTSViewPager;
import com.wbao.dianniu.utils.Utils;
import com.wbao.dianniu.R;

/**
 * 我的问答
 *
 * @author yejianjun
 */
public class MyQAnswerActivity extends FragmentActivity implements
        ViewPager.OnPageChangeListener, OnClickListener {

    private ImageButton backButton;
    public CustomPagerSlidingTabStrip2 mAPSTS;
    public APSTSViewPager mVP;
    private SendQaFragment friendsFragment = null;
    private PartInQaFragment recomFragment = null;
    private static final int VIEW_FIRST = 0;
    private static final int VIEW_SECOND = 1;

    private static final int VIEW_SIZE = 2;
    private FragmentAdapter adapter;
    private Context mContext;
    private TextView titleTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Utils.setTranslucentStatus(this, true);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintResource(R.color.title_bg);// 通知栏所需颜色
        }
        setContentView(R.layout.activity_my_qanswer);
        mContext = this;
        titleTV = (TextView) findViewById(R.id.title);
        titleTV.setText(getResources().getString(R.string.my_qa));
        backButton = (ImageButton) findViewById(R.id.back_button);
        backButton.setOnClickListener(this);

        mAPSTS = (CustomPagerSlidingTabStrip2) findViewById(R.id.qa_tab);
        mVP = (APSTSViewPager) findViewById(R.id.qa_apsts);
        mVP.setOffscreenPageLimit(VIEW_SIZE);

        adapter = new FragmentAdapter(getSupportFragmentManager());
        mVP.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        mAPSTS.setIndicatorHeight(4);
        mAPSTS.setViewPager(mVP);
        mAPSTS.setOnPageChangeListener(this);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    @Override
    public void onPageSelected(int arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_button:
                finish();
                break;

            default:
                break;
        }
    }

    private class FragmentAdapter extends FragmentStatePagerAdapter implements
            CustomPagerSlidingTabStrip2.CustomTabProvider {
        protected LayoutInflater mInflater;

        public FragmentAdapter(FragmentManager fm) {
            super(fm);
            mInflater = LayoutInflater.from(mContext);
        }

        @Override
        public Fragment getItem(int position) {
            // TODO Auto-generated method stub
            if (position >= 0 && position < VIEW_SIZE) {
                switch (position) {
                    case VIEW_FIRST:
                        if(null == friendsFragment)
                            friendsFragment = SendQaFragment.instance(mContext,SendQaFragment.TYPE_MYSELF, GlobalContext.getAccountId());
                        return friendsFragment;
                    case VIEW_SECOND:
                        if (null == recomFragment)
                            recomFragment = PartInQaFragment.instance(mContext);
                        return recomFragment;
                    default:
                        break;
                }
            }

            return null;
        }

        @Override
        public int getCount() {
            return VIEW_SIZE;
        }

        @Override
        public View getSelectTabView(int position, View convertView) {
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.custom_select_tab_2,
                        null);
            }
            TextView tv = ViewHolder.get(convertView, R.id.tvTab);
            tv.setText(getPageTitle(position));
            return convertView;
        }

        @Override
        public View getDisSelectTabView(int position, View convertView) {
            if (convertView == null) {
                convertView = mInflater.inflate(
                        R.layout.custom_disselect_tab_2, null);
            }
            TextView tv = ViewHolder.get(convertView, R.id.tvTab);
            tv.setText(getPageTitle(position));
            return convertView;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case VIEW_FIRST:
                    return getResources().getString(R.string.my_send_qa);
                case VIEW_SECOND:
                    return getResources().getString(R.string.my_partin_qa);
                default:
                    break;
            }

            return super.getPageTitle(position);
        }

    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
